package com.dhj.interfaceTest;

import com.dhj.streamTest.entity.Author;

import java.util.List;
import java.util.function.Predicate;

import static com.dhj.streamTest.entity.AuthorList.getAuthors;

/**
 * 年龄大于50，且有3本及以上书的作者
 */
public class Demo1 {

    public static void main(String[] args) {
        List<Author> authors = getAuthors();

        authors.stream().distinct()
                .filter(author -> author.getAge() > 50)
                .filter(author -> author.getBooks().size() > 2)
                .forEach(author -> System.out.println(author.getName()+"----"+author.getBooks().size()));

        System.out.println("------------------------------");

        authors.stream().distinct()
                .filter(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getAge() > 50;
                    }
                }.and(new Predicate<Author>() {
                    @Override
                    public boolean test(Author author) {
                        return author.getBooks().size() > 2;
                    }
                }))
                .forEach(author -> System.out.println(author.getName()+"-----"+author.getBooks().size()));


        System.out.println("------------------------------");

        authors.stream().distinct()
                .filter(((Predicate<Author>) author -> author.getAge() > 50).and(author -> author.getBooks().size() > 2))
                .forEach(author -> System.out.println(author.getName()+"-----"+author.getBooks().size()));


    }

}
